$(function() {



    //图片预览
    $('#inputCover').on('change', function() {
        let pic = this.files[0];
        let picUrl = URL.createObjectURL(pic);
        $('.article_cover').attr('src', picUrl)
    });

    //文章类别的请求
    $.ajax({
        type: 'get',
        url: BigNew.category_list,
        success: function(res) {
            console.log(res);
            if (res.code == 200) {
                let htmlStr = ''; +
                $.each(res.data, function(index, value) {
                    htmlStr += `<option value="${value.id}">${value.name}</option>`
                })
                $('.category').html(htmlStr)
            }
        }
    });



    //时间插件的显示
    $('.jebtns').click(function() {
        jeDate("#testico", {
            trigger: false,
            theme: { bgcolor: "#00A680", pnColor: "#00DDAA" },
            format: "YYYY-MM-DD hh:mm:ss",
            zIndex: 20990,
        });
    });

    //富文本事件
    const E = window.wangEditor
    const editor = new E('#div1')
        // 或者 const editor = new E( document.getElementById('div1') )
    editor.create()


    let dataId = window.location.search.split('=')[1]
    console.log(dataId);

    $.ajax({
        type: 'get',
        url: BigNew.article_search,
        data: {
            id: dataId
        },
        success: function(res) {
            console.log(res);
            if (res.code == 200) {
                $('#inputTitle').val(res.data.title);
                $('.article_cover').attr('src', res.data.cover);
                $('.category').val(res.data.categoryId);
                $('#testico').val(res.data.date);
                //富文本获取数据
                editor.txt.html(res.data.content);
            }
        }
    });


    function forData(state) {
        let fd = new FormData(document.querySelector('form'));
        fd.append('state', state);
        fd.append('id', dataId);
        fd.append('content', editor.txt.html())
        $.ajax({
            type: 'post',
            url: BigNew.article_edit,
            data: fd,
            contentType: false,
            processData: false,
            success: function(res) {
                alert(res.msg)
                console.log(res);
                // alert(res.msg);
                window.location.href = './article_list.html'
            }

        })
    }

    $('.btn-edit').click(function(e) {
        e.preventDefault();
        forData('已发布');

    })
    $('.btn-draft').click(function(e) {
        e.preventDefault();
        forData('草稿');
    })
})